<script setup lang="ts">
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import type { FormInstance } from "element-plus";
import http from "../utils/http";
import config from "../config/index.js";

const router = useRouter();
const registerFormRef = ref<FormInstance>();
const loading = ref(false);

const registerForm = reactive({
  username: "",
  password: "",
  confirmPassword: ""
});

// 确认密码验证
const validateConfirmPassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'));
  } else {
    callback();
  }
};

const registerRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, validator: validateConfirmPassword, trigger: "blur" }
  ]
};

const handleRegister = async () => {
  if (!registerFormRef.value) return;

  try {
    await registerFormRef.value.validate();
    loading.value = true;

    const res = await http.post("/register", {
      username: registerForm.username,
      password: registerForm.password
    });
    
    if (res.code === 200) {
      ElMessage.success("注册成功，请登录");
      await router.push("/login");
    } else {
      ElMessage.error(res.message || "注册失败");
    }
  } catch (error) {
    console.error("注册失败:", error);
    ElMessage.error("注册失败，请稍后重试");
  } finally {
    loading.value = false;
  }
};

// 跳转到登录页面
const goToLogin = () => {
  router.push("/login");
};
</script>

<template>
  <div class="register-container">
    <!-- 左侧背景图片区域 -->
    <div class="register-left">
      <div class="left-content">
        <div>
          <img src="@/assets/logo.png" alt="logo" class="logo">
        </div>
        <h1 class="system-title">非物质文化遗产保护平台</h1>
        <p class="system-slogan">传承千年匠心·守护文化瑰宝</p>
        <div class="feature-stats">
          <div class="stat-item">
            <span class="stat-number">1000+</span>
            <span class="stat-label">非遗项目</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">500+</span>
            <span class="stat-label">传承人</span>
          </div>
          <div class="stat-item">
            <span class="stat-number">30+</span>
            <span class="stat-label">省市覆盖</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 右侧注册表单 -->
    <div class="register-right">
      <div class="form-container">
        <div class="register-header">
          <h2 class="form-title">欢迎注册</h2>
          <div class="register-subtitle">非物质文化遗产保护平台</div>
        </div>

          <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            label-width="0"
            size="large"
          >
            <el-form-item prop="username">
              <el-input
                v-model="registerForm.username"
                placeholder="账号"
                :prefix-icon="User"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                placeholder="密码"
                :prefix-icon="Lock"
                show-password
              />
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                placeholder="确认密码"
                :prefix-icon="Lock"
                show-password
                @keyup.enter="handleRegister"
              />
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                :loading="loading"
                class="register-button"
                @click="handleRegister"
              >
                立即注册
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-footer">
            <div class="login-link">
              <span>已有账号？</span>
              <el-button type="text" @click="goToLogin">立即登录</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
</template>

<style scoped>
/* 背景样式 */
.register-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  width: 100%;
  height: 100vh;
  z-index: 2000;
  overflow: hidden;
}

/* 左侧背景图片区域 */
.register-left {
  width: 60%;
  background: url("/public/images/bg.jpg") no-repeat center center;
  background-size: cover;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.register-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
}

.left-content {
  position: relative;
  z-index: 1;
  color: white;
  text-align: center;
}

.system-title {
  font-size: 36px;
  margin-bottom: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.system-slogan {
  font-size: 18px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.feature-stats {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin-top: 40px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 16px;
  opacity: 0.9;
}

/* 右侧注册表单 */
.register-right {
  width: 40%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
  padding: 40px;
}

.form-container {
  width: 100%;
  max-width: 350px;
  padding: 20px;
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-title {
  font-size: 24px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 600;
  position: relative;
  display: inline-block;
}

.form-title::after {
  content: '🌟';
  margin-left: 8px;
  font-size: 20px;
}

.register-subtitle {
  font-size: 16px;
  color: #666;
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  margin-top: 20px;
  background-color: #ff4757;
  border-color: #ff4757;
}

.register-button:hover {
  background-color: #ff3838;
  border-color: #ff3838;
}

.form-footer {
  margin-top: 30px;
  text-align: center;
}

.login-link {
  font-size: 14px;
  color: #666;
}

.login-link span {
  margin-right: 8px;
}

.login-link .el-button--text {
  color: #ff4757;
}

/* 输入框样式优化 */
:deep(.el-input__wrapper) {
  height: 48px;
  border-radius: 8px;
  box-shadow: none;
  border: 1px solid #e0e0e0;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: none;
  border-color: #d0d0d0;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: none;
  border-color: #ff4757;
}

:deep(.el-input__inner) {
  height: 100%;
  font-size: 15px;
}

:deep(.el-input__prefix-inner) {
  margin-right: 10px;
}

/* 动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.register-container {
  animation: fadeIn 0.5s ease-out;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .register-container {
    flex-direction: column;
  }

  .register-left,
  .register-right {
    width: 100%;
  }

  .register-left {
    height: 40%;
    padding: 20px;
  }

  .system-title {
    font-size: 28px;
  }

  .feature-stats {
    gap: 20px;
  }

  .stat-number {
    font-size: 24px;
  }

  .register-right {
    height: 60%;
  }
}

@media (max-width: 576px) {
  .system-title {
    font-size: 24px;
  }

  .system-slogan {
    font-size: 16px;
  }

  .feature-stats {
    flex-direction: column;
    gap: 10px;
  }

  .form-container {
    padding: 10px;
  }
}
</style>